﻿/*+===================================================================
File:      StoneFalling.cpp

Summary:   Định nghĩa các phương thức của CStoneFalling.
===================================================================+*/

#include "StoneFalling.h"

#define TIME_RINGING 2.0f
#define VX -80.0f

CStoneFalling::CStoneFalling(D3DXVECTOR2 pos, int id, int typeID) : CMoveableObject(pos, id, typeID)
{
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_STONE_FALLING);
	m_pSprite = new CSprite(*m_pResourceManager->GetSprite(ID_SPRITE_STONE_FALLING));
	m_posInit = pos;
	m_pos = pos;
	m_fTimeRing = TIME_RINGING;
	m_v.x = VX;
	m_fScaleX = 1.0f;
	m_fScaleY = 1.0f;
	m_bIsFalling = false;

	m_box.width = 48;
	m_box.height = 54;
}

CStoneFalling::~CStoneFalling()
{
	if (m_lastGround)
		delete m_lastGround;
}

void CStoneFalling::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	if (m_fTimeRing > 0)
	{
		m_fTimeRing -= timeFrame;

		if (m_fTimeRingAnimation > 0)
		{
			m_fTimeRingAnimation -= timeFrame;
		}
		else
		{
			m_fTimeRingAnimation = 0.05f;
			m_v.x *= -1;
		}
	}
	else if (!m_bIsFalling)
	{
		m_a.y = -400.0f;
		m_v.x = 0.0f;
		m_bIsFalling = true;
	}

	if (m_bIsFalling)
	{
		if (m_fTimeScale > 0)
			m_fTimeScale -= timeFrame;
		else
		{
			m_fTimeScale = 0.06f;

			if (this->m_fScaleX > 0)
			{
				if (this->m_fScaleY > 0)
					this->m_fScaleY = -1;
				else
					this->m_fScaleX = -1;
			}
			else
			{
				if (this->m_fScaleY < 0)
					this->m_fScaleY = 1;
				else
					this->m_fScaleX = 1;
			}
		}
	}

	UpdateBox(timeFrame);
}

void CStoneFalling::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive)
		return;

	float timeCollision = timeFrame;
	CGameObject* objCollision = NULL; // Đối tượng va chạm sớm nhất.
	float normalx = 0.0f, normaly = 0.0f;

	for (int i = 0; i < vecGameObjs.size(); i++)
	{
		float t = CheckCollision(vecGameObjs[i], normalx, normaly, timeFrame);

		if (t < timeFrame && t < timeCollision && normaly == 1)
		{
			// Tìm đối tượng và thời gian va chạm sớm nhất.
			timeCollision = t;
			objCollision = vecGameObjs[i];
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL && m_lastGround != objCollision
		&& (objCollision->GetTypeId() == ID_GRASS || objCollision->GetTypeId() == ID_GRASS_CAN_FALL))
	{
		m_v.y += m_a.y * timeCollision;
		m_v.y = 120.0f;

		m_lastGround = objCollision;
	}

	m_pos += m_v * timeFrame;
	m_v += m_a * timeFrame;
}

void CStoneFalling::Draw()
{
	if (!m_bIsLive && !m_pSprite)
		return;
	D3DXVECTOR2 pos = CViewport::GetInstance()->TransformMatrix(m_pos);
	m_pSprite->DrawFlip(pos, m_fScaleX, m_fScaleY);
}

void CStoneFalling::Reset()
{
	m_bIsLive = true;
	m_pos = m_posInit;
	m_a.y = 0.0f;
	m_v.x = VX;
	m_v.y = 0.0f;
	m_fScaleX = 1.0f;
	m_fScaleY = 1.0f;
	m_bIsFalling = false;

	m_fTimeRing = TIME_RINGING;
}

bool CStoneFalling::IsFalling()
{
	return m_bIsFalling;
}